package com.alfayun.fiscalriskanalysis.reportspec;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReportTemplateService {
	
	@Autowired
	private ReportTemplateRepository repository;
	
	public List<ReportTemplate> list() {
		return repository.findAll();
	}
	
	public ReportTemplate queryById(int id) throws NoSuchReportTemplateException {
		return repository.findById(id).orElseThrow(NoSuchReportTemplateException::new);
	}
	
	public ReportTemplate newReportTemplate(ReportTemplate reportTemplate) {
		return ReportTemplate.newReportTemplate(repository, reportTemplate);
	}

	public void deleteReportTemplate(int id) {
		repository.deleteById(id);
	}

	public ReportTemplate modifyReportTemplate(int id, ReportTemplate reportTemplate) throws NoSuchReportTemplateException {
		Optional<ReportTemplate> row = repository.findById(id);

		if (row.isPresent()) {
			return repository.save(reportTemplate);
		}

		throw new NoSuchReportTemplateException();
	}

}
